{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b3272c7d",
   "metadata": {},
   "source": [
    "* 2023-10-16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "92149d24",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前时钟的分钟为偶数\n"
     ]
    }
   ],
   "source": [
    "# 项目1\n",
    "from datetime import datetime\n",
    "\n",
    "odds = [  1, 3,  5,  7,  9, 11,13,15,17,19,\n",
    "             21,23,25,27,29,31,33,35,37,39,\n",
    "             41,43,45,47,49,51,53,55,57,59]\n",
    "\n",
    "right_this_minute = datetime.today().minute\n",
    "\n",
    "if right_this_minute in odds:\n",
    "    print(\"当前时钟的分钟为奇数\")\n",
    "else:\n",
    "    print(\"当前时钟的分钟为偶数\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8488b0f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c1169b7b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2023"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.datetime.today().year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2b448855",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 补充random模块的介绍  \n",
    "# 随机函数 生成不大于1\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "93149b7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7540723072526722"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.random()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "47686822",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "64\n",
      "97\n",
      "89\n",
      "68\n",
      "70\n",
      "27\n",
      "86\n"
     ]
    }
   ],
   "source": [
    "# int取整\n",
    "for i in range(8):\n",
    "    print(int(random.random()*100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b4abb68b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.randint(1,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9a65845a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "3\n",
      "2\n",
      "1\n",
      "4\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# 5个1至6区间内的随机函数\n",
    "for i in range(6):\n",
    "    print(random.randint(1,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "026bef78",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前时钟的秒数为2,我们将等待1秒,此刻秒数为偶数\n",
      "当前时钟的秒数为3,我们将等待4秒,此刻秒数为奇数\n",
      "当前时钟的秒数为7,我们将等待1秒,此刻秒数为奇数\n",
      "当前时钟的秒数为8,我们将等待4秒,此刻秒数为偶数\n",
      "当前时钟的秒数为12,我们将等待1秒,此刻秒数为偶数\n"
     ]
    }
   ],
   "source": [
    "# 项目2\n",
    "from datetime import datetime\n",
    "\n",
    "import random\n",
    "import time\n",
    "\n",
    "odds = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59]\n",
    "\n",
    "for i in range(5): # for + range 可以决定程序执行的 次数\n",
    "    right_this_second = datetime.today().second  # 选择当前时钟的秒数\n",
    "    wait_time = random.randint(1,5)\n",
    "    if right_this_second in odds:\n",
    "        print(\"当前时钟的秒数为%s,我们将等待%s秒,此刻秒数为奇数\"%(right_this_second,wait_time))\n",
    "    else:\n",
    "        print(\"当前时钟的秒数为%s,我们将等待%s秒,此刻秒数为偶数\"%(right_this_second,wait_time))\n",
    "    \n",
    "    time.sleep(wait_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8dfef42a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "99 bottles 啤酒在墙上\n",
      "99 bottles 啤酒\n",
      "拿6瓶酒\n",
      "取走一次\n",
      "\n",
      "93 bottles 啤酒在墙上\n",
      "93 bottles 啤酒\n",
      "拿6瓶酒\n",
      "取走一次\n",
      "\n",
      "87 bottles 啤酒在墙上\n",
      "87 bottles 啤酒\n",
      "拿6瓶酒\n",
      "取走一次\n",
      "\n",
      "81 bottles 啤酒在墙上\n",
      "81 bottles 啤酒\n",
      "拿6瓶酒\n",
      "取走一次\n",
      "\n",
      "75 bottles 啤酒在墙上\n",
      "75 bottles 啤酒\n",
      "拿6瓶酒\n",
      "取走一次\n",
      "\n",
      "69 bottles 啤酒在墙上\n",
      "69 bottles 啤酒\n",
      "拿6瓶酒\n",
      "取走一次\n",
      "\n",
      "63 bottles 啤酒在墙上\n",
      "63 bottles 啤酒\n",
      "拿6瓶酒\n",
      "取走一次\n",
      "\n",
      "57 bottles 啤酒在墙上\n",
      "57 bottles 啤酒\n",
      "拿6瓶酒\n",
      "取走一次\n",
      "\n",
      "51 bottles 啤酒在墙上\n",
      "51 bottles 啤酒\n",
      "拿6瓶酒\n",
      "取走一次\n",
      "\n",
      "45 bottles 啤酒在墙上\n",
      "45 bottles 啤酒\n",
      "拿6瓶酒\n",
      "取走一次\n",
      "\n",
      "39 bottles 啤酒在墙上\n",
      "39 bottles 啤酒\n",
      "拿6瓶酒\n",
      "取走一次\n",
      "\n",
      "33 bottles 啤酒在墙上\n",
      "33 bottles 啤酒\n",
      "拿6瓶酒\n",
      "取走一次\n",
      "\n",
      "27 bottles 啤酒在墙上\n",
      "27 bottles 啤酒\n",
      "拿6瓶酒\n",
      "取走一次\n",
      "\n",
      "21 bottles 啤酒在墙上\n",
      "21 bottles 啤酒\n",
      "拿6瓶酒\n",
      "取走一次\n",
      "\n",
      "15 bottles 啤酒在墙上\n",
      "15 bottles 啤酒\n",
      "拿6瓶酒\n",
      "取走一次\n",
      "\n",
      "9 bottles 啤酒在墙上\n",
      "9 bottles 啤酒\n",
      "拿6瓶酒\n",
      "取走一次\n",
      "只有3瓶，不能拿了\n",
      "\n",
      "3 bottles 啤酒在墙上\n",
      "3 bottles 啤酒\n",
      "拿6瓶酒\n",
      "取走一次\n",
      "没酒了\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 项目3\n",
    "for beer_num in range(99, 0, -6):\n",
    "    print(beer_num,\"bottles\", \"啤酒在墙上\")\n",
    "    print(beer_num,\"bottles\", \"啤酒\")\n",
    "    print(\"拿6瓶酒\")\n",
    "    print(\"取走一次\")\n",
    "    if beer_num == 3:\n",
    "        print(\"没酒了\")\n",
    "    else:\n",
    "        new_num = beer_num - 6\n",
    "        if new_num == 3:\n",
    "            print(\"只有3瓶，不能拿了\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d4a8d24",
   "metadata": {},
   "source": [
    "# while 循环\n",
    "\n",
    "## while True 无限循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "af768d3b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是风儿我是沙，缠缠绵绵到天涯… 1\n",
      "你是风儿我是沙，缠缠绵绵到天涯… 2\n",
      "你是风儿我是沙，缠缠绵绵到天涯… 3\n",
      "你是风儿我是沙，缠缠绵绵到天涯… 4\n",
      "你是风儿我是沙，缠缠绵绵到天涯… 5\n"
     ]
    }
   ],
   "source": [
    "count = 0 # 计数器\n",
    "while True: \n",
    "    count +=1\n",
    "    print(\"你是风儿我是沙，缠缠绵绵到天涯…\",count)\n",
    "    if count ==5:\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d39fdd6",
   "metadata": {},
   "source": [
    "* 笔记：\n",
    "> 1. while循环往往和 True 一起使用\n",
    "> 2. while True 如果没有给定确定的条件，将无限循环下去\n",
    "> 3. while True 可以按照一定的条件执行break程序退出循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4f863652",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请写上你想要输入的数字：6\n",
      "你有4次机会来玩这个游戏\n",
      "请输入你猜的数字：3\n",
      "往大了猜！\n",
      "你有3次机会来玩这个游戏\n",
      "请输入你猜的数字：7\n",
      "数值比你的数小！\n",
      "你有2次机会来玩这个游戏\n",
      "请输入你猜的数字：5\n",
      "往大了猜！\n",
      "你有1次机会来玩这个游戏\n",
      "请输入你猜的数字：6\n",
      "你真棒！猜对啦！\n"
     ]
    }
   ],
   "source": [
    "## 猜数字\n",
    "\n",
    "# 1. 拿数字给别人猜\n",
    "hide_card = input(\"请写上你想要输入的数字：\")\n",
    "\n",
    "# 设置猜游戏的次数\n",
    "count = 5\n",
    "\n",
    "while True:\n",
    "    count -=1\n",
    "    # 2. 猜数字的人来猜\n",
    "    print(\"你有%s次机会来玩这个游戏\"%(count))\n",
    "    if count == 0:\n",
    "        print(\"游戏失败！\")\n",
    "        break\n",
    "        \n",
    "    else: \n",
    "        guess_digit = input(\"请输入你猜的数字：\")\n",
    "        # 情景一：猜对了\n",
    "        if hide_card == guess_digit:\n",
    "            print(\"你真棒！猜对啦！\")\n",
    "            break\n",
    "        # 情景二：猜错了\n",
    "        else:\n",
    "            if hide_card > guess_digit:\n",
    "                print(\"往大了猜！\")\n",
    "            elif hide_card < guess_digit:\n",
    "                print(\"数值比你的数小！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43d8db4c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
